package com.lingshi.shopping_category_service.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingshi.common.constant.RedisKey;
import com.lingshi.common.entity.Category;
import com.lingshi.common.service.CategoryService;
import com.lingshi.shopping_category_service.mapper.CategoryMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Arrays;
import java.util.List;

@DubboService
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private RedisTemplate redisTemplate;


    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void add(Category category) {
        categoryMapper.insert(category);
        syncCategoryToRedis();
    }

    @Override
    public void update(Category category) {
        categoryMapper.updateById(category);
        syncCategoryToRedis();
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        Category category = categoryMapper.selectById(id);
        category.setStatus(status);
        categoryMapper.updateById(category);
        syncCategoryToRedis();
    }

    @Override
    public void delete(Long[] ids) {

        categoryMapper.deleteBatchIds(Arrays.asList(ids));
        syncCategoryToRedis();
    }

    @Override
    public Category findById(Long id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public Page<Category> search(Integer page, Integer size) {
        return categoryMapper.selectPage(new Page<>(page,size),null);
    }

    @Override
    public List<Category> findAll() {
        //获取redis操作list集合对象
        ListOperations<String,Category> los = redisTemplate.opsForList();

        List<Category> categorys = los.range(RedisKey.CATEGORYS, 0, -1);

        if (CollUtil.isNotEmpty(categorys)) {
            System.out.println("从redis中获取");
            return categorys;
        }else {
            //从数据库查询所有启用的广告
            LambdaUpdateWrapper<Category> queryWrapper = new LambdaUpdateWrapper();
            queryWrapper.eq(Category::getStatus,1);
            List<Category> categories = categoryMapper.selectList(queryWrapper);
            return categories;
        }
    }

    /**
     * 同步mysql数据到redis
     */
    public void syncCategoryToRedis(){
        //从数据库查询所有启用的广告
        LambdaUpdateWrapper<Category> queryWrapper = new LambdaUpdateWrapper();
        queryWrapper.eq(Category::getStatus,1);
        List<Category> categories = categoryMapper.selectList(queryWrapper);

        ListOperations los = redisTemplate.opsForList();
        //删除rides原有的数据-删除key
        redisTemplate.delete(RedisKey.CATEGORYS);

        //将数据存储到redis
        los.leftPushAll(RedisKey.CATEGORYS,categories);
    }


}
